#pragma once
#include "log.hpp"
#include <iostream>
#include <cstring>
#include "Util.hpp"
#include <sstream>


#define SEP "\r\n"
#define SEP_LEN strlen(SEP)

const std::string default_root = "./wwwroot";
const std::string home_page = "index.html";

// string recvreq(int sock)
// {
//     char inbuffer[1024];
//     for (;;)
//     {
//         int ret = recv(sock, &inbuffer, sizeof(inbuffer), 0);
//         if (ret == -1)
//         {
//             logMessage(ERROR, "RECV ERROR");
//             exit(-1);
//         }
//         string buffer = inbuffer;
//         string text_len = buffer.substr(0, buffer.find(SEP));
//         int text_len_ = stoi(text_len);
//     }

//     return;
// }

struct request
{
    request() {}

    request(const std::string &packet)
        : _packet(packet)
    {
    }

    void parse()
    {
        std::string line = util::getOneline(_packet);
        std::stringstream sstr(line);
        sstr >> Get >> url >> httpversion;
        path += default_root;
        path += url;
        if(path[path.size() - 1] == '/')
            path += home_page;
    }

    ~request()
    {
    }

public:
    std::string _packet;
    std::string Get;
    std::string url;
    std::string httpversion;
    std::string path;
};

struct response
{
    std::string outbuffer;
};